const app = getApp();
const util = require('../../utils/util.js');
Page({
    data: {
        isClick: true,
        userInfo: {},
        deposit: 0,
        tel_: 0
    },
    onLoad() {
        // 获取用户信息
        my.showLoading({
            content: 'loading...'
        });
        this.getUserInfo();
        my.hideLoading();
    },
    onShow() {
        my.showLoading({
            content: 'loading...'
        });
        this.getUserInfo();
        my.hideLoading();
    },
    // =============== 获取用户信息 ====================
    getUserInfo() {
        let that = this;
        util.httpRequest('/getInfo', {}, function (res) {
            if (res.code == 1) {
                that.setData({
                    userInfo: res.data.user,
                    deposit: res.data.deposit,
                    tel_: res.data.tel_
                })
            }
        })
    },
    // ============== 执行退押金操作 =====================
    returnDeposit() {
        let that = this;
        if (that.data.isClick) {
            that.setData({
                isClick: false
            })
            if (that.data.deposit <= 0) {
                my.showToast({
                    content: '您不存在押金！',
                    type: 'none'
                });
                that.setData({
                    isClick: true
                })
            } else {
                my.confirm({
                    title: '退押金提醒',
                    content: '是否退押金？',
                    success: (res) => {
                        that.setData({
                            isClick: true
                        })
                        if (res.confirm) {
                            // my.showLoading({
                            //     content: 'loading...'
                            // });
                            util.httpRequest('/returnDeposit', {}, function (res) {
                                my.hideLoading();
                                if (res.code == 1) {
                                    my.showToast({
                                        content: `恭喜，退款成功,您的押金预计在1-3个工作日原路退回您的支付账户，请注意查收！`,
                                        type: 'none'
                                    });
                                    // 重新获取用户信息
                                    that.getUserInfo();
                                }
                            })
                        }
                    },
                });
            }
        }
    },
    // ========================= 客服电话 =============================
    makePhoneCall(e) {
        let tel = e.currentTarget.dataset.tel;
        my.makePhoneCall({number: tel.toString()});
    }
});
